package com.hslxy.niukewang.service;

import com.hslxy.niukewang.dao.MessageMapper;
import com.hslxy.niukewang.entity.Message;
import com.hslxy.niukewang.entity.User;
import com.hslxy.niukewang.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.util.HtmlUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.fasterxml.jackson.databind.type.LogicalType.Collection;

@Service
public class MessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private SensitiveFilter sensitiveFilter;

    //    查询回话列表
    public List<Message> findConversations(int userId, int offset, int limit){
        return messageMapper.selectConversations(userId, offset, limit);
    }
    //    查询当前用户的会话数量
    public int findConversationCount(int userId){
        return messageMapper.selectConversationCount(userId);
    }
    //    查询某个会话
    public List<Message> findLetters(String conversationId, int offset, int limit){
        return messageMapper.selectLetters(conversationId, offset, limit);
    }
    //    查询回话数量
    public int findLetterCount(String conversationId){
        return messageMapper.selectLetterCount(conversationId);
    }
    //    查询未读私信数量
    public int findLetterUnreadCount(int userId, String conversationId){
        return messageMapper.selectLetterUnreadCount(userId, conversationId);
    }

    //    新增消息
    public int addMessage(Message message){
        message.setContent(HtmlUtils.htmlEscape(sensitiveFilter.filter(message.getContent())));
        return messageMapper.insertMessage(message);
    }

    //    修改消息状态
    public int readMessage(List<Integer> ids){
        return messageMapper.updateStatus(ids, 1);
    }

    public int deleteMessage(int id){
        return messageMapper.updateStatus(Collections.singletonList(id), 2);
    }

//获取未读列表的ids
    public List<Integer> getUnreadLetterIds(List<Message> letterList, User user){
        List<Integer> ids = new ArrayList<>();
        if(letterList != null){
            for (Message message: letterList){
                if(user.getId() == message.getToId() && message.getStatus()==0){
                    ids.add(message.getId());
                }
            }
        }
        return ids;
    }
    //    查询某个主题下的最新通知
    public Message findLatestNotice(int userId, String topic){
        return messageMapper.selectLatestNotice(userId, topic);
    }
    //    查询某个主题所包含的通知数量
    public int findNoticeCount(int userId, String topic){
        return messageMapper.selectNoticeCount(userId, topic);
    }

    //    查询未读的通知数量
    public int findNoticeUnreadCount(int userId, String topic){
        return messageMapper.selectNoticeUnreadCount(userId, topic);
    }

    //    查询未读的通知数量
    public List<Message> findNotices(int userId, String topic, int offset, int limit){
        return messageMapper.selectNotices(userId, topic, offset, limit);
    }

}
